/******************** (C) ------------------------------------ ***************************
 * �ļ���  ��Button.c
 * ����    ���������ú�����   
 * ʵ��ƽ̨�� 
 * ��汾  �� 
 * ����    �� 
 * ����    ��
 *�޸�ʱ�� �� 

		Ӳ������
    |--------------------|
    |   KEY		-        |
    |   POWER   -        |
    |--------------------|

****************************************************************************************/

#include "key.h"
#include <lin_device.h>
#include "user_array.h"
#include <flash_device.h>

sKey Fun_S1;	
sKey Fun_S2;	
sKey Fun_S3;
sKey Fun_S4;

sDisplayTaskTypeDef led_task1;
sDisplayTaskTypeDef led_task2;
sDisplayTaskTypeDef led_task3;
sDisplayTaskTypeDef led_task4;

sFlagTypeDef sFlag;

  
uint32_t val;

extern LIN_Device_Frame_t linm_cmd_table[];

uint8_t TaskIndex = TASK_LIST_0;

  
void KeyScanExpired(SoftTimer_t *timer)
{
	sFlag.keysan_flag = ENABLE;
}


SoftTimer_t KeyScanTimer = {
  .mode 	= TIMER_PERIODIC_MODE,
  .interval = 10,
  .handler	= KeyScanExpired
};


void LED_TASK_Expired(SoftTimer_t *timer)
{
	if (led_task1.Task_DelayCount != 0)
	{
	 	led_task1.Task_DelayCount--;
	 	if (led_task1.Task_DelayCount == 0)
	 	{
	  		led_task1.Led_Task_Flag = ENABLE;
	 	}
	}
	if (led_task2.Task_DelayCount != 0)
	{
	 	led_task2.Task_DelayCount--;
	 	if (led_task2.Task_DelayCount == 0)
	 	{
	  		led_task2.Led_Task_Flag = ENABLE;
	 	}
	}
	if (led_task3.Task_DelayCount != 0)
	{
	 	led_task3.Task_DelayCount--;
	 	if (led_task3.Task_DelayCount == 0)
	 	{
	   		led_task3.Led_Task_Flag = ENABLE;
	 	}
	}
	if (led_task4.Task_DelayCount != 0)
	{
	 	led_task4.Task_DelayCount--;
	 	if (led_task4.Task_DelayCount == 0)
	 	{
	   		led_task4.Led_Task_Flag = ENABLE;
	 	}
	}
}

   
SoftTimer_t LED_TASK_Timer = {
	.mode	   = TIMER_PERIODIC_MODE,
	.interval = 10,
	.handler  = LED_TASK_Expired
	}; 

 /************************************************************************************
* FunctionName :KeyInit() 
* Description : ������ʼ������
* EntryParameter : None 
* ReturnValue : None 
***********************************************************************************/
void KeyInit(void)
{	
	SoftTimer_Start(&KeyScanTimer);
//	SoftTimer_Start(&LED_TASK_Timer);

	Key_Config(&Fun_S1, GPIO_PORT_1);
	Key_Config(&Fun_S2, GPIO_PORT_2);
	Key_Config(&Fun_S3, GPIO_PORT_3);
	Key_Config(&Fun_S4, GPIO_PORT_4);
	
#if 0
	LedTask_Config(&led_task1);
	LedTask_Config(&led_task2);
	LedTask_Config(&led_task3);
	LedTask_Config(&led_task4);
#endif
	
	FlagStruct_Init(&sFlag);


#if 0
	val = *((uint32_t*)FLASH_START_ADDRESS_USER_DATA); 	//��flash
	switch ( val )
	{
	    case 0 :
	        Fun_S1.keyvalue = ePress;
	        break;
	    case 1 :
	        Fun_S2.keyvalue = ePress;
	        break;
	    case 2 :
	        Fun_S3.keyvalue = ePress;
	        break;
	    case 3 :
	        Fun_S4.keyvalue = ePress;
	        break;
	    default:
			
			Flash_EraseSector(FLASH_START_ADDRESS_USER_DATA);
			Flash_WriteWord(FLASH_START_ADDRESS_USER_DATA,0);
	        Fun_S1.keyvalue = ePress;
	        break;
	}


	KeyPressHandle();
#endif

}
//-------------------------------------------------------------
void Task_KeyScan(void)
{
	if (sFlag.keysan_flag == ENABLE)
	{
		sFlag.keysan_flag = DISABLE;
		
		Key_GetKeyValue(&Fun_S1); 
		Key_GetKeyValue(&Fun_S2); 
		Key_GetKeyValue(&Fun_S3); 
		Key_GetKeyValue(&Fun_S4); 
		
		KeyPressHandle();
	}

}

void Task_Display(void)
{
	switch ( TaskIndex )
	{
	    case TASK_LIST_1 :
	        Display_Task_1();
	        break;
	    case TASK_LIST_2 :
	        Display_Task_2();
	        break;
	    case TASK_LIST_3 :
	        Display_Task_3();
	        break;		
		case TASK_LIST_4 :
			Display_Task_4();
			break;
	}
}

void Display_Task_1(void)//��
{
	if (led_task1.Led_Task_Flag == ENABLE)
	{
		led_task1.Led_Task_Flag = DISABLE;
		
		#if 0
		linm_cmd_table[0].frame_id = Task1_DataArray[led_task1.Task_ArrayCount*9+0];	

		linm_cmd_table[0].data[0] = Task1_DataArray[led_task1.Task_ArrayCount*9+1];		
		linm_cmd_table[0].data[1] = Task1_DataArray[led_task1.Task_ArrayCount*9+2];		
		linm_cmd_table[0].data[2] = Task1_DataArray[led_task1.Task_ArrayCount*9+3];		
		linm_cmd_table[0].data[3] = Task1_DataArray[led_task1.Task_ArrayCount*9+4];		
		linm_cmd_table[0].data[4] = Task1_DataArray[led_task1.Task_ArrayCount*9+5];		
		linm_cmd_table[0].data[5] = Task1_DataArray[led_task1.Task_ArrayCount*9+6];		
		linm_cmd_table[0].data[6] = Task1_DataArray[led_task1.Task_ArrayCount*9+7];		
		linm_cmd_table[0].data[7] = Task1_DataArray[led_task1.Task_ArrayCount*9+8];	

		LINM_SendFrame(&linm_cmd_table[0]);
		
		led_task1.Task_DelayCount = Task1_TimerArray[led_task1.Task_ArrayCount];
		led_task1.Task_ArrayCount++;
		if (led_task1.Task_ArrayCount > led_task1.Task_ArrayLen)
			led_task1.Task_ArrayCount = 0;
		#endif
		LINM_SendFrame(&linm_cmd_table[1]);

		
	}
}
void Display_Task_2(void)
{
	if (led_task2.Led_Task_Flag == ENABLE)
	{
		led_task2.Led_Task_Flag = DISABLE;
		
		linm_cmd_table[0].frame_id = Task2_DataArray[led_task2.Task_ArrayCount*9+0];	

		linm_cmd_table[0].data[0] = Task2_DataArray[led_task2.Task_ArrayCount*9+1];		
		linm_cmd_table[0].data[1] = Task2_DataArray[led_task2.Task_ArrayCount*9+2];		
		linm_cmd_table[0].data[2] = Task2_DataArray[led_task2.Task_ArrayCount*9+3];		
		linm_cmd_table[0].data[3] = Task2_DataArray[led_task2.Task_ArrayCount*9+4];		
		linm_cmd_table[0].data[4] = Task2_DataArray[led_task2.Task_ArrayCount*9+5];		
		linm_cmd_table[0].data[5] = Task2_DataArray[led_task2.Task_ArrayCount*9+6];		
		linm_cmd_table[0].data[6] = Task2_DataArray[led_task2.Task_ArrayCount*9+7];		
		linm_cmd_table[0].data[7] = Task2_DataArray[led_task2.Task_ArrayCount*9+8];		
		LINM_SendFrame(&linm_cmd_table[0]);

		
		led_task2.Task_DelayCount = Task2_TimerArray[led_task2.Task_ArrayCount];
		led_task2.Task_ArrayCount++;
		if (led_task2.Task_ArrayCount > led_task2.Task_ArrayLen)
			led_task2.Task_ArrayCount = 0;
	}
}
void Display_Task_3(void)
{
	if (led_task3.Led_Task_Flag == ENABLE)
	{
		led_task3.Led_Task_Flag = DISABLE;

		linm_cmd_table[0].frame_id = Task3_DataArray[led_task3.Task_ArrayCount*9+0];	

		linm_cmd_table[0].data[0] = Task3_DataArray[led_task3.Task_ArrayCount*9+1]; 	
		linm_cmd_table[0].data[1] = Task3_DataArray[led_task3.Task_ArrayCount*9+2]; 	
		linm_cmd_table[0].data[2] = Task3_DataArray[led_task3.Task_ArrayCount*9+3]; 	
		linm_cmd_table[0].data[3] = Task3_DataArray[led_task3.Task_ArrayCount*9+4]; 	
		linm_cmd_table[0].data[4] = Task3_DataArray[led_task3.Task_ArrayCount*9+5]; 	
		linm_cmd_table[0].data[5] = Task3_DataArray[led_task3.Task_ArrayCount*9+6]; 	
		linm_cmd_table[0].data[6] = Task3_DataArray[led_task3.Task_ArrayCount*9+7]; 	
		linm_cmd_table[0].data[7] = Task3_DataArray[led_task3.Task_ArrayCount*9+8]; 	
		LINM_SendFrame(&linm_cmd_table[0]);


		led_task3.Task_DelayCount = Task3_TimerArray[led_task3.Task_ArrayCount];
		led_task3.Task_ArrayCount++;
		if (led_task3.Task_ArrayCount > led_task3.Task_ArrayLen)
			led_task3.Task_ArrayCount = 0;
	}
}
void Display_Task_4(void)
{
	if (led_task4.Led_Task_Flag == ENABLE)
	{
		led_task4.Led_Task_Flag = DISABLE;

		linm_cmd_table[0].frame_id = Task4_DataArray[led_task4.Task_ArrayCount*9+0];	

		linm_cmd_table[0].data[0] = Task4_DataArray[led_task4.Task_ArrayCount*9+1];		
		linm_cmd_table[0].data[1] = Task4_DataArray[led_task4.Task_ArrayCount*9+2];		
		linm_cmd_table[0].data[2] = Task4_DataArray[led_task4.Task_ArrayCount*9+3];		
		linm_cmd_table[0].data[3] = Task4_DataArray[led_task4.Task_ArrayCount*9+4];		
		linm_cmd_table[0].data[4] = Task4_DataArray[led_task4.Task_ArrayCount*9+5];		
		linm_cmd_table[0].data[5] = Task4_DataArray[led_task4.Task_ArrayCount*9+6];		
		linm_cmd_table[0].data[6] = Task4_DataArray[led_task4.Task_ArrayCount*9+7];		
		linm_cmd_table[0].data[7] = Task4_DataArray[led_task4.Task_ArrayCount*9+8];		
		LINM_SendFrame(&linm_cmd_table[0]);

		
		led_task4.Task_DelayCount = Task4_TimerArray[led_task4.Task_ArrayCount];
		led_task4.Task_ArrayCount++;
		if (led_task4.Task_ArrayCount > led_task4.Task_ArrayLen)
			led_task4.Task_ArrayCount = 0;
	}
}

void Reset_Task_DelayCount(void)
{
	led_task1.Task_DelayCount = 0;
	led_task2.Task_DelayCount = 0;
	led_task3.Task_DelayCount = 0;
	led_task4.Task_DelayCount = 0;

}
void KeyPressHandle(void)
{ 	
  if (Fun_S1.keyvalue == ePress)
	{
		linm_cmd_table[0].data[0] = (linm_cmd_table[0].data[0]&0xFE) + (linm_cmd_table[0].data[0] & 0x01) ^ 0x01;		
	}
	if (Fun_S2.keyvalue == ePress)
	{
        linm_cmd_table[0].data[0] = (linm_cmd_table[0].data[0]&0xFD) + (linm_cmd_table[0].data[0] & 0x02) ^ 0x02;		
	}
	if (Fun_S3.keyvalue == ePress)
	{
        linm_cmd_table[0].data[0] = (linm_cmd_table[0].data[0]&0xFB) + (linm_cmd_table[0].data[0] & 0x04) ^ 0x04;	
	}
	if (Fun_S4.keyvalue == ePress)
	{
        linm_cmd_table[0].data[0] = (linm_cmd_table[0].data[0]&0xF7) + (linm_cmd_table[0].data[0] & 0x08) ^ 0x08;	
	}

}

#if 0
void KeyPressHandle(void)
{ 	
	if (Fun_S1.keyvalue == ePress)//
	{
	#if 0
		Flash_EraseSector(FLASH_START_ADDRESS_USER_DATA);
		Flash_WriteWord(FLASH_START_ADDRESS_USER_DATA,0);
		led_task1.Task_ArrayLen = 1; 
	#endif
		TaskIndex = TASK_LIST_1;
		led_task1.Led_Task_Flag = ENABLE;
		Reset_Task_DelayCount();
	}
	if (Fun_S2.keyvalue == ePress)//
	{
		Flash_EraseSector(FLASH_START_ADDRESS_USER_DATA);
		Flash_WriteWord(FLASH_START_ADDRESS_USER_DATA,1);
		led_task2.Task_ArrayLen = 1; 
		TaskIndex = TASK_LIST_2;
		led_task2.Led_Task_Flag = ENABLE;
		led_task2.Task_ArrayCount = 0;
		Reset_Task_DelayCount();
	}
	if (Fun_S3.keyvalue == ePress)//
	{
		Flash_EraseSector(FLASH_START_ADDRESS_USER_DATA);
		Flash_WriteWord(FLASH_START_ADDRESS_USER_DATA,2);
		led_task3.Task_ArrayLen = 1; 
		TaskIndex = TASK_LIST_3;
		led_task3.Led_Task_Flag = ENABLE;
		led_task3.Task_ArrayCount = 0;
		Reset_Task_DelayCount();
	}
	if (Fun_S4.keyvalue == ePress)//
	{
		Flash_EraseSector(FLASH_START_ADDRESS_USER_DATA);
		Flash_WriteWord(FLASH_START_ADDRESS_USER_DATA,3);
		led_task4.Task_ArrayLen = 29; 
		TaskIndex = TASK_LIST_4;
		led_task4.Led_Task_Flag = ENABLE;
		led_task4.Task_ArrayCount = 0;
		Reset_Task_DelayCount();
	}

}
#endif


/************************************************************************************
* FunctionName :Key_GetKeyValue() 
* Description : ��ȡ��ֵ
* EntryParameter : None 
* ReturnValue : None 
***********************************************************************************/
void Key_GetKeyValue(sKey* pKey)   
{   
	uint8_t eFlag = 1;//��Ч��ƽ
	uint8_t KeyFlag;
	
	eKeyResult Key_return = eNoClick;
	
	KeyFlag = GPIO_Read(pKey->gpioPin) ;
	
	KeyFlag = KeyFlag & 0x01;

//	KeyFlag = (pKey->gpioGroup->IDR & pKey->gpioPin) ?1 :0;                  // ������I/O��ƽ 

    
    switch (pKey->scanstatus)   
    {   
        case eKeyState0:                            // ������ʼ̬ 
            if (KeyFlag != eFlag) 
				pKey->scanstatus = eKeyState1;      // �������£�״̬ת��������������ȷ��״̬ 
            break;   
        case eKeyState1:                            // ����������ȷ��̬ 
            if (KeyFlag != eFlag)               //�а�������   
            {   
				Key_return = ePress;		        // ȷ�ϰ�������
                pKey->holdcount = 0;                    
                pKey->scanstatus = eKeyState2;      // ������Ȼ���ڰ��£�������ɣ�״̬ת�������¼�ʱ��ļ�ʱ״̬�������صĻ����޼��¼� 
            }   
            else
            	pKey->scanstatus = eKeyState0;      // ������̧��ת����������ʼ̬���˴���ɺ�ʵ��������������ʵ�����İ��º��ͷŶ��ڴ������ġ� 
            break;   
		case eKeyState2:   
			if (KeyFlag != eFlag)               // �������£���ʱ��10ms��10msΪ������ѭ��ִ�м���� 
			{	
				Key_return = eHoldPress;
				pKey->scanstatus = eKeyState2;	    // ת�����ȴ������ͷ�״̬ 
				pKey->holdcount++;
			}
			else
			{	
				Key_return = eRelease;
				pKey->scanstatus = eKeyState0;	    // ת����������ʼ̬ 
			}
			break;	 
    }   
	pKey->keyvalue = Key_return;
}   


/******************* (C) -------------------------------------------- *****END OF FILE****/
